import 'package:flutter/material.dart';
import 'package:homepad/models/weather_model.dart';
import 'package:homepad/styles.dart';
import 'package:provider/provider.dart';

class HomeWeatherWidget extends StatelessWidget {
  const HomeWeatherWidget({super.key});

  @override
  Widget build(BuildContext context) {
    final MyStyles styles = Theme.of(context).extension<MyStyles>()!;

    var info = context.select<WeatherModel, WeatherInfo?>((model) => model.info);
    if (info == null) {
      return const SizedBox();
    }
    final now = DateTime.now();
    final icon = switch(info.weather) {
      "晴"  => "sun",
      "少云" => "stars_moon",
      "阴" => "cloud",
      "晴间多云" => "cloud_sun",
      "多云" => now.hour > 18 && now.hour < 6 ? "cloud_moon" : "cloud_sun",
      "有风" || "平静" || "微风" || "和风" || "清风" || "强风/劲风" || "疾风" || "大风" || "烈风" || "风暴" || "狂爆风" || "飓风" || "热带风暴" => "wind",
      "霾" || "中度霾" || "重度霾" || "严重霾" => "cloud_fog",
      "阵雨" || "雷阵雨" || "雷阵雨并伴有冰雹" => "cloud_bolt_rain",
      "毛毛雨/细雨" || "小雨" => "cloud_drizzle",
      "雨" || "中雨" || "大雨" => "cloud_rain",
      "暴雨" || "大暴雨" || "特大暴雨" || "强阵雨" || "强雷阵雨" || "极端降雨" => "cloud_heavyrain",
      "雨雪天气" || "雨夹雪" || "阵雨夹雪" || "冻雨" || "雪" || "阵雪" || "小雪" || "中雪" || "大雪" || "暴雪" || "小雪-中雪" || "中雪-大雪" || "大雪-暴雪" => "cloud_snow",
      "浮尘" || "扬沙" || "沙尘暴" || "强沙尘暴" || "龙卷风" || "雾" || "浓雾" || "强浓雾" || "轻雾" || "大雾" || "特强浓雾" => "cloud_fog",
      _ => "cloud",
    };

    return Container(
      padding: const EdgeInsets.only(bottom: 6),
      height: 130,
      width: 100,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image(
              width: 70,
              height: 70,
              fit: BoxFit.fill,
              image: AssetImage("assets/images/weather/$icon.png")),
          Text("${info.temperature}°",
              textAlign: TextAlign.center,
              style: styles.homeWeatherTemperatureTextStyle.copyWith(
                height: 1,
              )),
        ],
      ),
    );
  }
}
